<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<jsp:include page="/WEB-INF/views/layouts/header.jsp" />
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	// 网站地址
	String netPath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort();
%>
<style>
.yhgltable ul li span.current {
	font-weight: bold
}
</style>
<script type="text/javascript">

	// 总页数
	var pageSum = 1;
	// 页码
	var pageNo = 1;
	// 搜索关键字
	var searchText = "";

	function toInsert(){
		document.location="<%=basePath%>/product/createProduct"
	}
	/**
	把指定的一组checkbox所选的值构造为逗号分隔的字符串
	*/
	function getAllValue(group_box) {
		var boxes = document.getElementsByName(group_box);
		var values = "";
		for (var i = 0; i < boxes.length; i++) {
			if (boxes[i].checked) {
				values += boxes[i].value + ",";
			}
		}
		if (values.length > 0) {
			values = values.substring(0, values.length - 1);
		}
		return values;
	}
	function deleteProduct(){
		var operatorIds = getAllValue("group_box");
		if(!operatorIds){
			alert("请选择需要删除的用户！");
			return;
		}
		$.ajax({
		  url: "<%=basePath%>/product/delProduct",
		  type:"POST",
		  contentType:"application/x-www-form-urlencoded; charset=utf-8",
		  data: "operatorIds="+operatorIds,
		  dataType: 'text',
		  success: function(data) {
			 var jsonobj=eval('('+data+')');
			 if(jsonobj.state==='1'){
				 alert(jsonobj.result);
				 var searchText = encodeURIComponent($("[name=searchText]").val()?$("[name=searchText]").val():"");
				 var currentPage = $("[name=currentPage]").val()?parseInt($("[name=currentPage]").val()):1;
				 var departmentId = $("[name=departmentId]").val()?$("[name=departmentId]").val():"";
				 if(currentPage>=1){
					$(".userContent").load("<%=basePath%>/product/info?departmentId="+departmentId+"&searchText="+searchText+"&page="+currentPage+"&rd="+Math.random());
				 }
			 }else if(jsonobj.state==='0'){
				 alert(jsonobj.result);
			 }
		  },
		  error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert("删除失败！");
		  }
		});
	}
	function toUpdate(){
		var boxes = document.getElementsByName("group_box");
		for (var i = 0; i < boxes.length; i++) {
			if (boxes[i].checked) {
				var operatorId = boxes[i].value;
				break;
			}
		}
		if(!operatorId) {
			alert("请选择需要修改的产品！");
			return ;
		}
		window.location="<%=basePath%>/product/update/"+operatorId;
	}
	function initPageButton(){
		if(pageSum>1){
			if(pageNo==1){
				//默认不可用
				$("[name=prevBut]").addClass("prev")
				$("[name=next]").removeClass("next");
			} else if(pageNo==pageSum){
				//下翻页不可用
				$("[name=prevBut]").addClass("next")
				$("[name=prev]").removeClass("prev");
				alert($("[name=prevButton]").attr("class"));
			} else {
				// 上下翻页都可用
				$("[name=prevBut]").removeClass("prev");
				$("[name=next]").removeClass("next");
				
			}
		} else {
			$("[name=prevBut]").addClass("prev");
			$("[name=next]").addClass("next");
		}
	};
	
		// 局部刷新
	function refreshProduct(){
	
	     if(searchText==""){
	    	searchText = $("[name=searchText]").val()?$("[name=searchText]").val():"";
	    }
		$(".userContent").load("<%=basePath%>product/filterProduct?searchText="+ searchText + "&pageNo="+ pageNo,
		     function(response,status){
      		if (status=="success"){
      		//alert('success');
      			var intPageSum = parseInt($(response).find("input[name='pageSum']").val());
      			alert(iniPageSum);
      			if(intPageSum>=1){
      			pageSum = intPageSum;
      			}
      			initPageButton();
      		}
        });
	};
	
function search(){
		pageNo = 1;
		pageSum = 1;
		searchText = $("[name=searchText]").val()?encodeURIComponent($("[name=searchText]").val()):"";
		// 让ie不读取缓存
		$.ajaxSetup ({ cache: false });
		$(".userContent").load("<%=basePath%>product/filterProduct?searchText="+searchText+"&pageNo="+pageNo,function(response,status){
      		if (status=="success"){
      			var intPageSum = parseInt($(response).find("input[name='pageSum']").val());
      			//alert(intPageSum);
      			if(intPageSum>=1){
      				pageSum = intPageSum;
      			}
      			initPageButton();
      			//alert("pageNo:"+pageNo+":pageSum:"+pageSum);
      		}
        });
	};
	// 下一页
	function nextPage(){
		if(pageNo<pageSum){
			pageNo ++;
			initPageButton();
			//alert("pageNo:" + pageNo + "  pageSum:" + pageSum);
			refreshProduct();
		}
	//	alert(pageNo);
	};
	
	function prevPage(){
		
		if(pageNo>1){
			pageNo --;
			initPageButton();
			//alert("pageNo:" + pageNo + "  pageSum:" + pageSum);
			refreshProduct();
			
		}
	//	alert(pageNo);
	};

function makeTop(tid){
       // alert(tid);
		var iftop = "#iftop_" + tid;
	  //  alert(iftop);
		//alert($(iftop).val());
		var topValue = parseInt($(iftop).val());
		if(topValue<30){
			topValue ++;
			//$(iftop).val(topValue);
			changeTopStat(tid,topValue);
		}
	};
	// 置顶操作
	function makeLower(tid){
		var iftop = "#iftop_" + tid;
		var topValue = parseInt($(iftop).val());
		if(topValue>0){
			topValue --;
			//$(iftop).val(topValue);
			changeTopStat(tid,topValue);
		}
	};	
	// 異步提交
	function changeTopStat(tid,topValue){
		$.ajax({
			  url: "<%=basePath%>product/makeTop",
			  type:"POST",
			  contentType:"application/x-www-form-urlencoded; charset=utf-8",
			  data: {"id":tid,"ifTop":topValue},
			  dataType: 'text',
			  success: function(data) {
				 var jsonobj=eval('('+data+')');
				// alert(jsonobj.state);
				 if(jsonobj.state==='1'){
					var iftop = "#iftop_" + tid;
					 // alert(topValue);
					$(iftop).val(topValue);
					alert(jsonobj.result);
				 } else if(jsonobj.state==='0'){
					 alert(jsonobj.result);
				 }
			  },
			  error: function(XMLHttpRequest, textStatus, errorThrown) {
					 alert("修改失败！");
			  }
		});
	};
	
	  //推荐
  function changeRecommendStat(tid){

	  var recommendItem = "#recommendItem_" + tid;
	  var recommendImg = "#recommendImg_" + tid;
	  var recommendValue =  $(recommendItem).val();

	  // 取消推荐
	  if(recommendValue == 1){
		  recommendValue = 0;
		  $(recommendImg).attr("src","<%=basePath%>images/commends0.png");
		  $(recommendImg).attr("title","推荐");
		  //$(recommendItem).val(recommendValue);
	  } else {
	  // 推荐
		  recommendValue = 1;
		  $(recommendImg).attr("src","<%=basePath%>images/commends1.png");
		  $(recommendImg).attr("title","取消推荐");
		  //$(recommendItem).val(recommendValue);
	  }

	  $.ajax({
		  url: "<%=basePath%>product/ifRecommend",
		  type:"POST",
		  contentType:"application/x-www-form-urlencoded; charset=utf-8",
		  data: {"id":tid,"recommendValue":recommendValue},
		  dataType: 'text',
		  success: function(data) {
			 var jsonobj=eval('('+data+')');
			 if(jsonobj.state==='1'){
				$(recommendItem).val(recommendValue);
				alert(jsonobj.result);
			 } else if(jsonobj.state==='0'){
				 alert(jsonobj.result);
			 }
		  },
		  error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert("修改失败！");
		  }
		});

 	};
 	function makeShow(sid){	
 	        alert(sid);
 	       var ifShow = "#ifShow_" + sid;
 	       var showValue = parseInt($(ifShow).val()); 
		   changeShow(sid,showValue);
	};
 	
 	// 異步提交
	function changeShow(sid,showValue){
		$.ajax({
			  url: "<%=basePath%>product/ifShow",
			  type:"POST",
			  contentType:"application/x-www-form-urlencoded; charset=utf-8",
			  data: {"id":sid,"ifShow":showValue},
			  dataType: 'text',
			  success: function(data) {
				 var jsonobj=eval('('+data+')');
				// alert(jsonobj.state);
				 if(jsonobj.state==='1'){
					alert(jsonobj.result);
				 } else if(jsonobj.state==='0'){
					 alert(jsonobj.result);
				 }
			  },
			  error: function(XMLHttpRequest, textStatus, errorThrown) {
					 alert("修改失败！");
			  }
		});
	};
 	
 	function checkFile(){
 	   var vt = $("#file").val();
 	   if(vt == ""){
 	      alert("请您选择要导入的文件！");
 	     return false;
 	   }
 	}
 	
 	function modify(operatorId){
 	  //  alert(operatorId);
 	  if(operatorId != ""){
		window.location="<%=basePath%>/product/update/"+operatorId;
		}else{
		  alert("此产品不存在！");
		}
	}
</script>
<!--中间部分开始-->
<div class="main">
	<!-- 左侧 -->
	<jsp:include page="/WEB-INF/views/account/sidebar.jsp" />
	<!-- /左侧 -->
	<div id="iaas_layout_content" class="rightblock">
		<p class="shadow"></p>
		<div class="content">
			<!--默认状态下引用样式ml60，展开后引用样式ml140-->
			<div class="mainblock" style="height: 650px;">
				<div class="tabA">
					<h1>产品管理</h1>
				</div>
				<div class="lineone mt10">
					<span><img src="<%=basePath%>images/back.png" name="prevBut"
						class="prev linkhand" style="cursor:pointer;" onclick="prevPage()" /><img
						src="<%=basePath%>images/next.png" class="next linkhand"
						name="next" style="cursor:pointer;" onclick="nextPage()" /> </span>
					<div class="btn">
						<a href="javascript:void(0)" onclick="toInsert()"><img
							src="<%=basePath%>images/xz.gif" />新增</a> <a
							href="javascript:void(0)" onclick="deleteProduct()"><img
							src="<%=basePath%>images/del.gif" />删除</a> <a
							href="javascript:void(0)" onclick="toUpdate()"><img
							src="<%=basePath%>images/xg.gif" />修改</a>
					</div>
					<div class="search">
						<input type="text" name="searchText" placeholder="请输产品名称或关键字查询" />
						<img src="<%=basePath%>images/search.png" onclick="search(this)"
							class="linkhand" />
					</div>
					<div style="width:700px;height:28px;">
						<form id="readReportForm" action="<%=basePath%>product/readProduct" method="post" enctype="multipart/form-data" onsubmit="return checkFile();">
							<label title="请您下载批量上传导入模板，按要求填写数据，在进行导入" for="file">批量上传理财产品：</label><input id="file" type="file"
								style="border:thin solid #BBB;background: url(../images/search_bg.gif) repeat-x;" name="file" />
							<input type="submit" value="提交" style="margin-right: 10px;  border: 1px solid #BBB;background: url(../images/btn_bg.gif) repeat-x;padding: 0px 13px;line-height: 28px;color: #666;"/><a herf="">下载模板</a>
					     <a href="<%=basePath%>/product/reInfo">首页产品</a>
					    </form>
					   
					</div>
                   
					<table border="0" width="100%" height="100%" cellpadding="0"
						cellspacing="0" class="yhgltable mt10">
						<tr>
							<td rowspan="2" class="userContent" valign="top"><script>$(".userContent").load("<%=basePath%>product/showInfo",function(response,status){
			            		if (status=="success"){
			            		    //   alert(response);
			            				var intPageSum = parseInt($(response).find("input[name='pageSum']").val());
			            				//alert(intPageSum);
			            				if(intPageSum>=1){
			            					pageSum = intPageSum;
			            				}
			            				initPageButton();
			            			//alert(pageSum);
			            		}
			            		
			            	});
			            	</script></td>
						</tr>
						<!--   <tr>
			           <td valign="top">
			            <ul class="dm-menu">
			              </ul>
			             <script>
			              		var jsonArray = eval('${bankFinanceProduct}');
			              		if(jsonArray){
			              			$(".dm-menu").html(buildTree(jsonArray));
			              		}
			              		function buildTree(jsonArray){
			              			var html = "";
			              			html = buildNode(jsonArray[0]);
			              			return html;
			              		}
			              </script>
			              </td> 
			          </tr>-->
					</table>
					<!-- <div class="lineone mt10">
					<span><img src="../../../images/back.png"
						class="prev linkhand" style="cursor:pointer;" onclick="prevPage()" /><img
						src="../../../images/next.png" class="next linkhand"
						style="cursor:pointer;" onclick="nextPage()" />
					</span>
					
					
				</div> -->
				</div>
			</div>
		</div>
	</div>
	<!--底部开始-->
	<jsp:include page="/WEB-INF/views/layouts/footer.jsp" />
	<!--底部结束-->
	</body>
	</html>